package com.dj.mingkong.portal.biz.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dj.mingkong.common.annotation.RequestLimit;
import com.dj.mingkong.common.infrastructure.base.dto.page.PageVo;
import com.dj.mingkong.common.infrastructure.base.dto.rest.RestResponse;
import com.dj.mingkong.common.microservice.UserContext;
import com.dj.mingkong.common.util.AssertUtils;
import com.dj.mingkong.common.util.SmartBeanCopy;
import com.dj.mingkong.portal.biz.controller.system.vo.request.UserFansAddVo;
import com.dj.mingkong.portal.biz.controller.system.vo.request.UserFansQueryVo;
import com.dj.mingkong.portal.biz.controller.system.vo.response.UserFansDetailVo;
import com.dj.mingkong.portal.biz.controller.system.vo.response.UserFansVo;
import com.dj.mingkong.portal.biz.service.system.UserFansService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 用户粉丝管理
 *
 * @author zenglonghui
 * @date: 2022/8/25 11:27
 */
@RestController
@RequestMapping("/user/fans")
public class UserFansController {
    @Resource
    private UserFansService userFansService;

    /**
     * 关注用户
     *
     * @param addVo 实例对象
     * @return 是否成功，true-成功，false-失败
     */
    @PostMapping("/follow")
    @RequestLimit(count = 10)
    public RestResponse<Boolean> follow(@RequestBody @Validated UserFansAddVo addVo) {
        userFansService.follow(addVo);
        return RestResponse.buildSuccess();
    }

    /**
     * 获取用户关注信息
     *
     * @param followUserId 被关注用户id
     * @return 是否成功，true-成功，false-失败
     */
    @GetMapping("/getUserFans/{followUserId}")
    public RestResponse<UserFansDetailVo> getUserFans(@PathVariable("followUserId") Long followUserId) {
        return RestResponse.success(userFansService.getUserFans(followUserId));
    }

    /**
     * 分页查询我关注的用户列表
     *
     * @param queryVo {}
     * @return UserVo
     */
    @PostMapping("/follow/pageByVo")
    public RestResponse<PageVo<UserFansVo>> pageFollowByVo(@RequestBody @Validated UserFansQueryVo queryVo) {
        AssertUtils.checkQueryPageVo(queryVo);
        queryVo.setUserId(UserContext.getUserId());
        final Page<UserFansVo> userFansVoPage = userFansService.pageFollowByVo(queryVo);
        final PageVo<UserFansVo> pageVo = SmartBeanCopy.copyPageVo(userFansVoPage, UserFansVo.class);
        return RestResponse.success(pageVo);
    }

    /**
     * 我的粉丝列表
     *
     * @param queryVo {}
     * @return UserFansVo
     */
    @PostMapping("/pageByVo")
    public RestResponse<PageVo<UserFansVo>> pageByVo(@RequestBody @Validated UserFansQueryVo queryVo) {
        AssertUtils.checkQueryPageVo(queryVo);
        queryVo.setUserId(UserContext.getUserId());
        final Page<UserFansVo> userFansVoPage = userFansService.pageByVo(queryVo);
        final PageVo<UserFansVo> pageVo = SmartBeanCopy.copyPageVo(userFansVoPage, UserFansVo.class);
        return RestResponse.success(pageVo);
    }

    /**
     * 取消关注
     *
     * @param userFansVo {}
     * @return Boolean
     */
    @PostMapping("/cancel")
    @RequestLimit(count = 5)
    public RestResponse<Boolean> cancel(@RequestBody @Validated UserFansVo userFansVo) {
        userFansVo.setUserId(UserContext.getUserId());
        userFansService.cancel(userFansVo);
        return RestResponse.buildSuccess();
    }
}
